/*
 * @Desc: 
 * @Author: 小超越
 * @LastEditors: 小超越
 * @BlogUrl: www.zhe94.com
 */
layui.use(['form', 'element', 'jquery', 'button'], function() {
    var form = layui.form;
    var element = layui.element;
    var button = layui.button;
    var $ = layui.jquery;
    // 登 录 提 交
    form.on('submit(login-submit)', function(data) {

        button.load({
            elem: '.login',
            time: 100,
            done: function() {
                login(data.field)
                return false;
            }
        })
    });
    function login(data) {
        $.ajax({
            data: data,
            type: "POST",
            dataType: "json",
            success: function(res) {
                if (res.code == 0) {

                    layer.msg(res.msg, {
                        icon: 1,
                    });
                    setTimeout(function() {
                        location.href = '/admin.php';
                    }, 100)
                } else {
                    layer.msg(res.msg, {
                        icon: 2,
                    });
                    initCode();
                }
            },
            error: function() {
                layer.msg('系统异常', {
                    icon: 0,
                });
                initCode();
            }
        });
    }
    initCode();
    document.onkeydown = function (e) { // 回车提交表单
            var theEvent = window.event || e;
            var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
            if (code == 13) {
                var data=form.val('login-form');
                login(data);
            }
        }
    function initCode() {
        $('#codeimg').attr("src", "verify?data=" + new Date().getTime());
    }
    $('#codeimg').on('click', function() {
        initCode();
    });
})